<script setup>
import { h,ref } from 'vue'
import { ElMessage,ElDivider } from 'element-plus'
import { Message, Lock } from '@element-plus/icons-vue'
import {useRouter} from 'vue-router'
import {login} from '@/api/user'
import { useTokenStore } from '@/stores/token'
const router = useRouter()
const tokenStore = useTokenStore()
const size = ref(2)
const spacer = h(ElDivider, { direction: 'vertical' })
// 登录表单数据
const loginForm = ref({
  username : '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = async() => {
  try {
    loading.value = true
    let result = await login(loginForm.value)
    tokenStore.setToken(result.data.token, result.data.student)
    ElMessage.success(result.msg || '登录成功')
    // 添加定时器，延迟跳转
    setTimeout(() => {
      router.push(result.data.student.role === 'admin' ? '/admin/dashboard' : '/student/home')
      loading.value = false
    }, 800) // 1秒后跳转
  } catch (error) {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧图片 -->
      <div class="login-image">
        <img src="@/assets/login.png" alt="智慧商城">
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-form">
        <h2 class="login-title">智慧宿舍管理</h2>
        <p class="login-subtitle">欢迎登录，请输入您的账号信息</p>
        
        <el-form
          :model="loginForm"
          :rules="rules"
          label-position="top"
          class="login-form-content"
          @keyup.enter="handleLogin"
        >
          <el-form-item prop="username" style="margin-top: 20px;">
            <el-input 
              v-model="loginForm.username"
              placeholder="请输入账号"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <div class="login-options">
            <div class="login-links">
              <el-space :size="size" :spacer="spacer">
                <el-link type="default" :underline="false" @click="router.push('/register')">注册账号</el-link>
              <el-link type="primary" :underline="false">忘记密码?</el-link>
              </el-space>
            </div>
          </div>
          
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  
  .login-box {
    display: flex;
    width: 1000px;
    height: 500px;
    background-color: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    
    .login-image {
      flex: 1.3;
      background-color: #f0f7ff;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 10px;
      img {
        max-width: 100%;
        max-height: 100%;
      }
    }
    
    .login-form {
      flex: 0.7;
      padding: 48px;
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .login-form-content {
        width: 100%;
        max-width: 320px;
      }
      
      .login-title {
        font-size: 32px;
        font-weight: 700;
        color: #1a1a1a;
        margin-bottom: 16px;
        text-align: center;
      }
      
      .login-subtitle {
        font-size: 16px;
        color: #666666;
        margin-bottom: 40px;
        text-align: center;
      }
      
      .login-options {
        display: flex;
        justify-content: left;
        align-items: center;
        margin-bottom: 25px;
        
        .login-links {
          display: flex;
          align-items: left;
          
          .divider {
            margin: 0 12px;
            color: #999293;
          }
        }
      }
      
      .login-button {
        width: 100%;
        height: 46px;
        font-size: 16px;
        font-weight: 600;
        letter-spacing: 1px;
        border-radius: 8px;
        margin-top: 8px;
      }
    }
  }
}


@media screen and (max-width: 768px) {
  .login-box {
    width: 92%;
    max-width: 480px;
    flex-direction: column;
    height: auto;
    margin: 20px;
    
    .login-image {
      display: none;
    }
  }
}
</style>